/*******************************************************************************
 * Copyright 2007-2014 FZI, http://www.fzi.de
 *                 Forschungszentrum Informatik - Information Process Engineering (IPE)
 *  
 *                 See the NOTICE file distributed with this work for additional
 *                 information regarding copyright ownership
 *                
 *                 Licensed under the Apache License, Version 2.0 (the "License");
 *                 you may not use this file except in compliance with the License.
 *                 You may obtain a copy of the License at
 *                
 *                   http://www.apache.org/licenses/LICENSE-2.0
 *                
 *                 Unless required by applicable law or agreed to in writing, software
 *                 distributed under the License is distributed on an "AS IS" BASIS,
 *                 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *                 See the License for the specific language governing permissions and
 *                 limitations under the License.
 * @author tzentek - <a href="mailto:zentek@fzi.de">Tom Zentek</a>
 * @author cyumusak - <a href="mailto:canyumusak@gmail.com">Can Yumusak</a>
 ******************************************************************************/
package de.fzi.fhemapi.view.vaadin.ui.core;

import java.lang.reflect.Constructor;
import java.util.LinkedList;
import java.util.List;

import de.fzi.fhemapi.model.devicetypes.Device;
import de.fzi.fhemapi.view.vaadin.ui.DeviceDetailsPanel;
import de.fzi.fhemapi.view.vaadin.ui.devicepanels.DeviceDetailsAbst;
import de.fzi.fhemapi.view.vaadin.ui.devicepanels.DeviceDetailsImpl;
import de.fzi.fhemapi.view.vaadin.ui.devicepanels.DeviceStateDetails;
import de.fzi.fhemapi.view.vaadin.ui.devicepanels.FS20Details;
import de.fzi.fhemapi.view.vaadin.ui.devicepanels.OnOffDetails;
import de.fzi.fhemapi.view.vaadin.ui.model.DeviceDetailEntry;

/**
 * This class manages the details of every device to show. One should register more java objects extending "DeviceDetailsAbst" 
 * if one wants to show more details on any device.
 * @author Can Yumusak
 *
 */
public class DevicePanelManager {
	
	private static DevicePanelManager instance;
	private List<Class<? extends DeviceDetailsAbst>> details;
	
	/**
	 * Default constructor.
	 */
	public DevicePanelManager() {
		initDefaultDeviceTypes();
	}
	
	private void initDefaultDeviceTypes(){
		details = new LinkedList<Class<? extends DeviceDetailsAbst>>();
		details.add(DeviceDetailsImpl.class);
		details.add(DeviceStateDetails.class);
		details.add(OnOffDetails.class);
		details.add(FS20Details.class);

	}
	
	/**
	 * Returns a list of device details based on a device and it's panel. 
	 * @param device the device to show.
	 * @param parent the devicedetailspanel which it should be shown on
	 * @return a List of DeviceDetailEntries. 
	 */
	public static List<DeviceDetailEntry> getDeviceDetails(Device device, DeviceDetailsPanel parent){
		List<DeviceDetailEntry> entries = new LinkedList<DeviceDetailEntry>();
		try{
			for(Class<? extends DeviceDetailsAbst> clazz : getInstance().details){
				Constructor<? extends DeviceDetailsAbst> constructor = clazz.getConstructor(Device.class, DeviceDetailsPanel.class);
				
				DeviceDetailsAbst details = constructor.newInstance(device, parent);
				if(details.matchesDevice()){
					List<DeviceDetailEntry> childs = details.getDeviceDetails();
					for(DeviceDetailEntry child : childs)
						entries.add(child);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return entries;
	}
	
	/**
	 * Returns the singleton instance of this class.
	 * @return the instance.
	 */
	public static DevicePanelManager getInstance() {
		if(instance == null)
			instance = new DevicePanelManager();
		return instance;
	}

	/**
	 * Registers a DeviceDetailsAbst class to the UI.
	 * @param detailsClass
	 */
	public static void registerDeviceDetails(Class<? extends DeviceDetailsAbst> detailsClass){
		getInstance().details.add(detailsClass);
	}
}
